package com.eiokey.linkbus.utils;


import android.app.Activity;
import android.app.Application;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;

import com.eiokey.linkbus.annotation.LinkBusAnnotationHandler;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * description
 * use
 * param
 * return
 * @author JetQiao
 * @date 2021/7/23  11:02
 */

public class LinkContentProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        Application app = (Application) getContext().getApplicationContext();
        app.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                if (activity instanceof LifecycleOwner) {
                    injectBus((LifecycleOwner) activity);
                }
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    /**
     * 初始化，注册到事件总线
     * @param lifecycleOwner 生命
     */
    private void injectBus(LifecycleOwner lifecycleOwner) {
        Method[] methods = lifecycleOwner.getClass().getDeclaredMethods();
        if (methods.length == 0) {
            return;
        }
        List<Method> allMethods = new ArrayList<>(Arrays.asList(methods));
        if (lifecycleOwner.getClass().getSuperclass() != null) {
            Method[] superMethods = lifecycleOwner.getClass().getSuperclass().getDeclaredMethods();
            if (superMethods.length != 0) {
                allMethods.addAll(Arrays.asList(superMethods));
            }
        }
        LinkBusAnnotationHandler handler = new LinkBusAnnotationHandler();
        for (Method method : allMethods) {

            Annotation[] annotations = method.getAnnotations();
            if (annotations.length == 0) {
                continue;
            }

            for (Annotation annotation : annotations) {
                handler.collectMethodAnnotation(method, annotation);
            }
        }
        handler.handleMethodAnnotation(lifecycleOwner);
    }
}

